<template>
  <div class="dashboard-container">
    <div class="dashboard-text">当前部门: {{ name }}</div>
    <div style="margin-top:20px; margin-bottom: 20px; margin-left: 20px">
    <el-form  :model="formData" :rules="rules" label-width="120px" style="height: 850px;width: 600px">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model="formData.name" :disabled="true" placeholder="1-20个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item prop="employeeNumber" label="部门人数">
        <el-input v-model="formData.employeeNumber" placeholder="请输入部门人数" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item prop="password" label="部门密码">
        <el-input v-model="formData.password" placeholder="请输入部门密码" style="width: 80%" size="mini" />
      </el-form-item>

      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="updateDepartmentDetail(formData)">确定</el-button>
            <!--<el-button size="mini" @click="close">取消</el-button>-->
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {getDepartmentDetailToUpdate,updateDepartmentDetail } from '@/api/department'
import store from '@/store'
export default {
  data() {
    return {
      levelList: [], // 存储负责人列表
      formData: {
        levelId: '', // 部门级别
        name: '', // 部门名称
        pid: '', // 父级部门的id
        password:'',
        employeeNumber: null
      },
      rules: {
        levelId: [{ required: true, message: '部门级别不能为空', trigger: 'blur' }], // 部门级别id
        employeeNumber: [{ required: true, message: '部门人数不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1, max: 30, message: '部门名称的长度为1-20个字符', trigger: 'blur'
          }, {
            trigger: 'blur',
            // 自定义校验模式
            validator: async(rule, value, callback) => {
              // value就是输入的编码
              const result = await getDepartment()
              // result数组中是否存在 value值
              if (result.some(item => item.name === value)) {
                callback(new Error('部门中已经有该名称了'))
              } else {
                callback()
              }
            }
          }] // 部门名称
        // pid: '' // 父级部门的id 不需要做校验
      },
      departmentName:null
    }
  },
  name: 'Dashboard',
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  methods: {
    async getDepartmentDetailToUpdate(departmentName){
      const data = {  name:departmentName}
      this.formData = await getDepartmentDetailToUpdate(data)
    },
    async updateDepartmentDetail(formData){

      await updateDepartmentDetail(formData)
      this.$message.success('修改成功！')
    }

  },
  created() {
    this.departmentName = store.getters.name;
    this.getDepartmentDetailToUpdate(this.departmentName);
  },
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
